  function [out_cell,out]=playmod_outdraws(drawsM,coefname,densmat,densnames); 
% function out_cell=playmod_outdraws(drawsM,coefname,densmat,densnames);
% 
%
% Use this function with playmod_crdraws or a matrix of draws in rows 
% Will take the draws and generate summary statistics
% Means, medians, stds and percentiles 
% Inputs 
% =======
% drawsM    numdr x nump 
% coefname  nump  x 1    cell of coefficient names 
%
% Optional (do not call otherwise)
% densmat and densnames 
% Matrix and cell of same size, will be appended to end of table 
% 
% Alejandro Justiniano 1/16/05 
%                      6/27/05
%                      4/23/07 Added coefname as a default
% ===========================================================
n=size(drawsM); 
if nargin < 2 | isempty(coefname)
    disp('Use default coefficient names') 
    coefname=fnumcell('coefficient ',[1:n(2)]); 
end 
coefname=coefname(:); 

if length( coefname )~=n(2);  
    error('Number of entries in COEFNAME and number of columns do not match') 
end 

med=median( drawsM )'; 
meand=mean( drawsM )'; 
stand=std( drawsM )'; 

out.med=med; 
out.mean=meand; 
out.std=stand; 

pvec   = [0.05 0.1 0.9 0.95]; 
drawsM = sort( drawsM ); 
exind  = round( n(1)*pvec ); 
drawsp = ( drawsM(exind,:) )'; 

out.p5=drawsp(:,1);out.p95=drawsp(:,end); 
out.p1=drawsp(:,2);out.p90=drawsp(:,end-1); 

disp(' '); 
disp(['Number of draws ',num2str(n(1)),' for ',num2str(n(2)),' parameters'] ); 

out_cell=num2cellaj( [ med meand stand drawsp] );
clear drawsp; 

out_cell=[coefname out_cell ]; 
temp=[{'Coefficient','Median','Mean','Std','5%','10%','90%','95%'}]; 
out_cell=[temp;out_cell]; 
clear temp; 

if nargin == 4 
    
    densmat=densmat(:);
    densnames = densnames(:);

    d = length( densmat );
    if d ~= length( densnames )
        error('DENSMAT does not match dimension of DENSNAMES')
    end
    temp = emptycell( d, 8  ); 
    temp(:,1) = densnames ; 
    temp(:,2) = num2cprec( densmat ); 
    
    temp2 = emptycell(1, 8); 
    temp2(1,1) ={'DENSITIES'}; 
    
    out_cell = [out_cell;temp2;temp]; 
    clear temp temp2; 
    
end 
tempc={'Num draws',num2str(n(1))}; 
out_cell=merge_cells(tempc,out_cell,1); 
disp(' '); 
disp('Coefficients Summary'); 
printcell(out_cell);